//
//  ViewController.m
//  14_UICollectionView
//
//  Created by dllo on 16/8/2.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "ViewController.h"
#define kCollectionViewCellReuse @"reuse"
#define kCollectionViewHeader @"HeaderReuseable"
#define kCollectionViewFooter @"FooterReuseable"
@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 1.itemSize(它的默认大小是{50, 50})
    layout.itemSize = CGSizeMake(CGRectGetWidth(self.view.bounds) / 2 - 7.5, 100);
    // 2.最小行间距
    layout.minimumLineSpacing = 5;
    // 3.最小列间距
    layout.minimumInteritemSpacing = 5;
    // 4.滚动方向的改变会交换行和列
    // 设置滚动方向(水平和竖直)
//    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    // 5.sectionInset
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    // 6.区头大小, 横向滚动时, 设置宽, 纵向滚动时, 设置高
    layout.headerReferenceSize = CGSizeMake(0, 100);
    // 7.区尾大小, 同上
    layout.footerReferenceSize = CGSizeMake(0, 200);
    // 8.区头悬浮效果(iOS9之后用)
    layout.sectionHeadersPinToVisibleBounds = YES;
    // 区尾悬浮效果
    layout.sectionFootersPinToVisibleBounds = YES;
    
    // UICollectionView的创建
    // 参数1:frame
    // 参数2:
//    self.view.backgroundColor = [UIColor redColor];
    UICollectionView *myCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    myCollectionView.dataSource = self;
    myCollectionView.delegate = self;
    [self.view addSubview:myCollectionView];
    [myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kCollectionViewCellReuse];
    [myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewHeader];
    
    [myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kCollectionViewFooter];


}
#pragma mark - [dataSource]分区的个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 10;
}
#pragma mark - [dataSource]每个分区的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 10;
}
#pragma mark -[dataSource协议方法]
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewCellReuse forIndexPath:indexPath];
    
    cell.contentView.backgroundColor = [UIColor redColor];
    return cell;
}
#pragma mark - [dataSource]区头区尾的协议方法
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *reuseableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewHeader forIndexPath:indexPath];
        reuseableView.backgroundColor = [UIColor orangeColor];
        return reuseableView;
    } else {
        UICollectionReusableView *reuseableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kCollectionViewFooter forIndexPath:indexPath];
        reuseableView.backgroundColor = [UIColor cyanColor];
        return reuseableView;
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
